package cn.the.angry.resourcemanager;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import cn.the.angry.model.IDs;

public class ResourcePool {
	
	public static final  String resourceDir = System.getProperty("user.dir")+"/res/pics/";
	
	public BufferedImage ingame_ground;
	public BufferedImage ingame_parallx;
	public BufferedImage ingame_skies;
	public BufferedImage ingame_birds;
	public BufferedImage cursor_sheet;
	public BufferedImage ingame_blocks1;
	public BufferedImage ingame_blocks2;
	public BufferedImage ingame_theme_ground1;
	public BufferedImage splash_screen;
	public BufferedImage cut_scenes;
	
	public ResourcePool(){
		loadResources();
	}
	
	//得到开始画面
	public static Image getSplashScreen(){
		return new ImageIcon(resourceDir+"SplashScreen.png").getImage();
	}
	//得到登录界面背景
	public static Image getLoginBackgroundImage(){
		return new ImageIcon(resourceDir+"loginback.jpg").getImage();
	}
	//得到登录按钮背景
	public static Image getLoginButtonImage(){
		return new ImageIcon(resourceDir+"loginbtn.png").getImage();
	}
	
	private void loadResources(){
		ingame_ground = getBufferedImageById(IDs.INGAME_GROUNDS_1);
		ingame_parallx = getBufferedImageById(IDs.INGAME_PARALLAX_1);
		ingame_skies = getBufferedImageById(IDs.INGAME_SKIES_1);
		ingame_birds = getBufferedImageById(IDs.INGAME_BIRDS_1);
		cursor_sheet = getBufferedImageById(IDs.CURSORS_SHEET_1);
		ingame_blocks1 = getBufferedImageById(IDs.INGAME_BLOCKS_1);
		ingame_blocks2 = getBufferedImageById(IDs.INGAME_BLOCKS_2);
		ingame_theme_ground1 = getBufferedImageById(IDs.INGAME_THEME_GROUND_1);
		cut_scenes = getBufferedImageById(IDs.CUTSCENES);
	}
	
	private Image getImageById(int id){
		return getImage(IDs.getResourceNameById(id));
	}
	
	private Image getImage(String filename){
		return new ImageIcon(resourceDir+filename).getImage();
	}
	
	public BufferedImage getBufferedImageById(int id){
		Image im = getImageById(id);
		int w = im.getWidth(null);
		int h = im.getHeight(null);
		BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		//反锯齿
		Graphics2D g2 = buf.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		buf = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
		buf = buf.createGraphics().getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
		buf.createGraphics().drawImage(im, 0, 0, null);
		return buf;
	}
	
	//需要添加的操作
	public void releaseResources(){
		
	}
}
